<template>
  <el-card>
    <el-button type="primary" icon="Plus" @click="addJob"> 新增职位 </el-button>
  </el-card>
  <el-card style="margin: 10px 0px">
    <el-table border :data="jobList" class="table">
      <el-table-column
        label="序号"
        type="index"
        align="center"
        width="80px"
      ></el-table-column>
      <el-table-column
        label="职位名称"
        prop="jName"
        align="center"
      ></el-table-column>
      <el-table-column
        label="职位描述"
        prop="jContent"
        align="center"
      ></el-table-column>
      <el-table-column label="创建时间" align="center">
        <template #default="{ row }">
          {{ row.createAt.split("T")[0] }}
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center">
        <template #default="{ row }">
          {{ row.updateAt.split("T")[0] }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="350px">
        <template #default="{ row }">
          <el-button
            type="primary"
            icon="Plus"
            size="small"
            @click="AddRole(row._id)"
            >分配权限</el-button
          >
          <el-button
            type="info"
            icon="edit"
            size="small"
            @click="UpdateJob(row._id)"
            >修改信息</el-button
          >
          <el-popconfirm :title="`你确定删除${row.jName}职位吗?`" @confirm="deleteJob(row._id)">
            <template #reference>
              <el-button type="danger" icon="delete" size="small" 
            >删除职位</el-button
          >
            </template>
          </el-popconfirm>
          <!-- 
         
          -->
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      class="page"
      v-model:current-page="pageNo"
      v-model:page-size="pageSzie"
      :page-sizes="[5, 10, 15, 20]"
      background
      layout=" prev, pager, next, jumper,total, sizes,"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-card>
  <el-dialog
    v-model="centerDialogVisible"
    :title="jobInfo.id ? '修改职位信息' : '新增职位'"
    width="500"
    center
    @close="closeDialog"
  >
    <el-form :model="jobInfo" :rules="rules" ref="form">
      <el-form-item label="职位名称" prop="jName">
        <el-input
          v-model="jobInfo.jName"
          placeholder="请输入职位名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="职位描述" prop="jContent">
        <el-input
          v-model="jobInfo.jContent"
          placeholder="请输入职位描述"
          clearable
          type="textarea"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="AddOrUpdateJob"> 确定 </el-button>
      </div>
    </template>
  </el-dialog>
  <el-drawer v-model="drawer" title="分配权限" @open="openDwarp">
    <el-checkbox
      v-model="checkAll"
      :indeterminate="isIndeterminate"
      @change="handleCheckAllChange"
      >全选</el-checkbox
    >
    <el-checkbox-group v-model="selectMenu" @change="handleCheckedCitiesChange">
      <el-checkbox
        v-for="city in menuList"
        :key="city._id"
        :label="city.mName"
        :value="city._id"
        >{{ city.mName }}</el-checkbox
      >
    </el-checkbox-group>
    <template #footer>
      <el-button type="danger">取消</el-button>
      <el-button type="primary" @click="comform">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup>
defineOptions({
  name: "job",
});
import { ref, reactive, onMounted, watch } from "vue";
import {
  getJobListAPI,
  AddJobAPI,
  getJobDetailAPI,
  addJobPermissionAPI,
  deleteJobAPI,
} from "@/api/job";
import { getMenuListAPI } from "@/api/menu";
import { ElMessage } from "element-plus";
let pageNo = ref(1);
let pageSzie = ref(5);
let total = ref(0);
let jobList = ref([]);
let form = ref(null);
let checkAll = ref(false);
let isIndeterminate = ref(false);
let jobInfo = reactive({
  jName: "",
  jContent: "",
});
let rules = reactive({
  jName: [
    {
      required: true,
      message: "请输入职位名称",
      trigger: "blur",
    },
    {
      min: 2,
      max: 10,
      message: "长度在2到10个字符",
      trigger: "blur",
    },
  ],
  jContent: [
    {
      required: true,
      message: "请输入职位描述",
      trigger: "blur",
    },
    {
      min: 2,
      max: 10,
      message: "长度在2到10个字符",
      trigger: "blur",
    },
  ],
});
let centerDialogVisible = ref(false);
let menuList = ref([]);
let selectMenu = ref([]);
let drawer = ref(false);
let addMenusToJob = reactive({
  jid: "",
  mids: [],
});
onMounted(() => {
  getJobList();
});
watch(
  () => selectMenu.value,
  (newVal) => {
    if (newVal.length > 1) {
      isIndeterminate.value = true;
    }
    if (newVal.length == menuList.value.length) {
      checkAll.value = true;
      isIndeterminate.value = false;
    }
    if (newVal.length == 0) {
      console.log(1);
      checkAll.value = false;
      isIndeterminate.value = false;
    }
  }
);
const getJobList = async () => {
  let result = await getJobListAPI(pageNo.value, pageSzie.value);
  jobList.value = result.data;
  total.value = result.total;
};
const handleSizeChange = () => {
  getJobList();
};
const handleCurrentChange = (val) => {
  pageNo.value = val;
  getJobList();
};
const addJob = () => {
  centerDialogVisible.value = true;
};
const AddOrUpdateJob = async () => {
  await form.value.validate();
  await AddJobAPI(jobInfo);
  ElMessage.success(jobInfo.id ? "修改成功" : "添加成功");
  closeDialog();
  getJobList();
};
const closeDialog = () => {
  centerDialogVisible.value = false;
  form.value.resetFields();
  Object.assign(jobInfo, { jName: "", jContent: "", id: "" });
};
const AddRole = async (id) => {
  drawer.value = true;
  addMenusToJob.jid = id;
  let result = await getJobDetailAPI(id);
  console.log(result);
  selectMenu.value = result.data.menus.map((item) => item.menuId);
};
const openDwarp = async () => {
  let result = await getMenuListAPI(1, 100);
  menuList.value = result.data;
};
const handleCheckedCitiesChange = (value) => {
  selectMenu.value = value;
  isIndeterminate.value = true;
  if (selectMenu.value.length === menuList.value.length) {
    isIndeterminate.value = false;
    checkAll.value = true;
  }
  if (selectMenu.value.length === 0) {
    checkAll.value = false;
    isIndeterminate.value = false;
  }
};
const handleCheckAllChange = (val) => {
  selectMenu.value = val ? menuList.value.map((item) => item._id) : [];
  isIndeterminate.value = false;
};
const comform = async () => {
  addMenusToJob.mids = selectMenu.value;
  await addJobPermissionAPI(addMenusToJob);
  ElMessage.success("分配权限成功");
  drawer.value = false;
  selectMenu.value = [];
  checkAll.value = false;
  isIndeterminate.value = false;
};
const UpdateJob = async (id) => {
  centerDialogVisible.value = true;
  let result = await getJobDetailAPI(id);
  jobInfo.jName = result.data.jName;
  jobInfo.jContent = result.data.jContent;
  jobInfo.id = result.data.jid;
};
const deleteJob = async (id) => {
  await deleteJobAPI(id);
  ElMessage.success("删除成功");
  getJobList();
};
</script>

<style lang="scss" scoped>
.table {
  margin: 10px 0px;
}
.page {
  display: flex;
  justify-content: center;
}
</style>
